import java.io.File;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.validation.SchemaFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class Song {
	private String title, author, copyright, ccli,
	capo, key, aka, key_line, user1, user2, user3,
	theme, tempo, time_sig;
	private String presentation; //make own class
	private String lyrics; //probably needs own class	
	
	private File file; //for referecing filename	
	
	/**
	 * @param filePath - path to song file
	 */
	public Song(String filePath) {
		file = new File(filePath);
		DocumentBuilder parser;
		try {
			parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
			Document dom = parser.parse(file);
	        XPath xpath = XPathFactory.newInstance().newXPath();
	        //Node songNode = (Node)xpath.evaluate("/song", dom, XPathConstants.NODE);

	        title = getText(xpath.evaluate("/song/title", dom, XPathConstants.NODE));
	        author = getText(xpath.evaluate("/song/author", dom, XPathConstants.NODE));
	        copyright = getText(xpath.evaluate("/song/copyright", dom, XPathConstants.NODE));
	        presentation = getText(xpath.evaluate("/song/presentation", dom, XPathConstants.NODE));
	        ccli = getText(xpath.evaluate("/song/ccli", dom, XPathConstants.NODE));
	        capo = getText(xpath.evaluate("/song/capo", dom, XPathConstants.NODE));
	        key = getText(xpath.evaluate("/song/key", dom, XPathConstants.NODE));
	        aka = getText(xpath.evaluate("/song/aka", dom, XPathConstants.NODE));
	        key_line = getText(xpath.evaluate("/song/key_line", dom, XPathConstants.NODE));
	        user1 = getText(xpath.evaluate("/song/user1", dom, XPathConstants.NODE));
	        user2 = getText(xpath.evaluate("/song/user2", dom, XPathConstants.NODE));
	        user3 = getText(xpath.evaluate("/song/user3", dom, XPathConstants.NODE));
	        theme = getText(xpath.evaluate("/song/theme", dom, XPathConstants.NODE));
	        tempo = getText(xpath.evaluate("/song/tempo", dom, XPathConstants.NODE));
	        time_sig = getText(xpath.evaluate("/song/time_sig", dom, XPathConstants.NODE));
	        lyrics = getText(xpath.evaluate("/song/lyrics", dom, XPathConstants.NODE));
	        
//	        NodeList list = songNode.getChildNodes();
//	        for (int i=0; i<list.getLength(); i++) {
//	        	System.out.println("Node: " + list.item(i));
//	        }
		} catch (ParserConfigurationException e) {
			// TODO newdocumentbuilder
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO parser.parse
			e.printStackTrace();
		} catch (IOException e) {
			// TODO parser.parse
			e.printStackTrace();
		} catch (XPathExpressionException e) {
			// TODO xpath expression fail
			e.printStackTrace();
		}
		
		
	}
	
	private String getText(Object o) {
		Node n = (Node)o;
		if (n == null || !n.hasChildNodes())
			return "";
		else if (n.getFirstChild() == null || n.getFirstChild().getTextContent() == null)
			return "";
		else
			return n.getFirstChild().getTextContent();
	}
	
	public String getTitle() {
		return title;
	}

	public String getAuthor() {
		return author;
	}

	public String getCopyright() {
		return copyright;
	}

	public String getCcli() {
		return ccli;
	}

	public String getCapo() {
		return capo;
	}

	public String getKey() {
		return key;
	}

	public String getAka() {
		return aka;
	}

	public String getKey_line() {
		return key_line;
	}

	public String getUser1() {
		return user1;
	}

	public String getUser2() {
		return user2;
	}

	public String getUser3() {
		return user3;
	}

	public String getTheme() {
		return theme;
	}

	public String getTempo() {
		return tempo;
	}

	public String getTime_sig() {
		return time_sig;
	}

	public String getPresentation() {
		return presentation;
	}

	public String getLyrics() {
		return lyrics;
	}

	public File getFile() {
		return file;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder(2048);
		sb.append(title + "(" + aka + ")\n");
		sb.append(author + "\n");
		sb.append(copyright + "\n");
		sb.append(presentation + "\n");
		sb.append(key + "\n");
		sb.append(capo + "\n");
		sb.append(tempo + "\n");
		sb.append(lyrics + "\n");
		
		return sb.toString();
	}
	
}
